#include "Tank.hpp"
#include "PhysicEngine.hpp"
#include "Utils.hpp"

Tank::Tank():
m_indexCannon(0),
m_indexGround(0),
m_color("green"),
m_position(),
m_life(5),
m_body(NULL)
{
	// create body
	b2BodyDef def;
	def.type = b2_dynamicBody;
	def.position = b2Vec2(105.f/40.f,500.f/40.f);
	m_body = PhysicEngine::instance().world()->CreateBody(&def);
	
	b2CircleShape shape;
	shape.m_radius = 25.f/40.f;
	b2FixtureDef fixDef;
	fixDef.shape = &shape;
	fixDef.restitution = 0.f;
	fixDef.friction = 0.5f;
	fixDef.density = 10.f;
	m_body->CreateFixture(&fixDef);
}		

Tank::Tank(const Tank& t):
m_indexCannon(t.m_indexCannon),
m_indexGround(t.m_indexGround),
m_color(t.m_color),
m_position(t.m_position),
m_life(t.m_life),
m_body(t.m_body)
{
}

Tank& Tank::operator=(const Tank& t)
{
	m_indexCannon = t.m_indexCannon;
	m_indexGround = t.m_indexGround;
	m_color = t.m_color;
	m_position = t.m_position;
	m_life = t.m_life;
	m_body = t.m_body;
	
	return *this;
}
		
Tank::~Tank()
{
	PhysicEngine::instance().world()->DestroyBody(m_body);
}

void Tank::setColor(const std::string& color)
{
	m_color = color;
}

ImagePtr Tank::cannonImage()
{
	TilesetPtr tileset = LOAD_TILESET("data/images/tank_cannon_part_" + m_color + ".dat");
	return tileset->tile(m_indexCannon);
}
ImagePtr Tank::groundImage()
{
	TilesetPtr tileset = LOAD_TILESET("data/images/tank_ground_part_" + m_color + ".dat");
	return tileset->tile(m_indexGround);
}

sf::Vector2f Tank::position() const
{
	return m_position;
}

bool Tank::isAlive() const
{
	return !m_life;
}
